import Vue from 'vue'

/**
 * enable 可点击指令
 */
const enable = Vue.directive('enable', {
  inserted: function (el, binding, vnode) {
    if (binding.value) {
      const fn = new Function('context', binding.value)
      const disabled = fn(vnode.context)
      setTimeout(() => {
        disabled ? el.setAttribute('disabled', 'disabled') : el.removeAttribute('disabled')
      }, 5)
    }
  },
  update: function (el, binding, vnode) {
    binding.def.inserted(el, binding, vnode)
  }
})

export default enable
